const FileHandler = {
  preview: function(filePath) {
    uni.showLoading({
      title: '正在加载中..'
    });

    const fileExtension = filePath.split('.').pop().toLowerCase();
    const imageExtensions = ['jpg', 'jpeg', 'png', 'gif', 'bmp'];

    if (imageExtensions.includes(fileExtension)) {
      // 处理图片文件
      if (filePath.startsWith('http')) {
        uni.downloadFile({
          url: filePath,
          success: function(res) {
            if (res.statusCode === 200) {
              uni.previewImage({
                current: res.tempFilePath,
                urls: [res.tempFilePath],
                success: function() {
                  console.log('文件打开成功');
                  uni.hideLoading();
                },
                fail: function(err) {
                  console.log('文件打开失败', err);
                  uni.hideLoading();
                }
              });
            } else {
              console.log('下载图片失败');
              uni.hideLoading();
            }
          },
          fail: function(err) {
            console.log('下载失败', err);
            uni.hideLoading();
          }
        });
      } else {
        uni.previewImage({
          current: filePath,
          urls: [filePath],
          success: function() {
            console.log('文件打开成功');
            uni.hideLoading();
          },
          fail: function(err) {
            console.log('文件打开失败', err);
            uni.hideLoading();
          }
        });
      }
    } else if (['pdf', 'docx', 'doc', 'xlsx', 'pptx'].includes(fileExtension)) {
      // 处理文档文件
      if (filePath.startsWith('http')) {
        uni.downloadFile({
          url: filePath,
          success: function(res) {
            if (res.statusCode === 200) {
              uni.openDocument({
                filePath: res.tempFilePath,
                showMenu: true,
                success: function() {
                  console.log('文件打开成功');
                  uni.hideLoading();
                },
                fail: function(err) {
                  console.log('文件打开失败', err);
                  uni.hideLoading();
                }
              });
            } else {
              console.log('下载文件失败');
              uni.hideLoading();
            }
          },
          fail: function(err) {
            console.log('下载失败', err);
            uni.hideLoading();
          }
        });
      } else {
        uni.openDocument({
          filePath: filePath,
          showMenu: true,
          success: function() {
            console.log('文件打开成功');
            uni.hideLoading();
          },
          fail: function(err) {
            console.log('文件打开失败', err);
            uni.hideLoading();
          }
        });
      }
    } else {
      uni.hideLoading();
      console.log('不支持的文件类型');
    }
  },

  download: function(filePath) {
    uni.showLoading({
      title: '正在加载中..'
    });

    const fileExtension = filePath.split('.').pop().toLowerCase();
    const imageExtensions = ['jpg', 'jpeg', 'png', 'gif', 'bmp'];

    if (imageExtensions.includes(fileExtension)) {
      // 下载图片文件
      uni.downloadFile({
        url: filePath,
        success: (res) => {
          uni.saveImageToPhotosAlbum({
            filePath: res.tempFilePath,
            success: () => {
              uni.hideLoading();
              uni.showToast({
                title: "图片已保存",
                icon: "success"
              });
            },
            fail: (err) => {
              uni.hideLoading();
              uni.showToast({
                title: "保存图片失败",
                icon: "error"
              });
            }
          });
        },
        fail: (err) => {
          uni.hideLoading();
          uni.showToast({
            title: "下载失败",
            icon: "error"
          });
        }
      });
    } else if (['pdf', 'docx', 'doc', 'xlsx', 'pptx'].includes(fileExtension)) {
      // 下载文档文件
      uni.downloadFile({
        url: filePath,
        success: function(res) {
          const fs = uni.getFileSystemManager();
          var filePaths = res.tempFilePath;
          var sFileName = filePaths.split('/').pop();
          var savePath = `${uni.env.USER_DATA_PATH}/${sFileName}`;

          fs.saveFile({
            tempFilePath: filePaths,
            filePath: savePath,
            showMenu: true,
            success: function(ress) {
              uni.hideLoading();
			  console.log(ress)
              if (ress.errMsg === 'saveFile:ok') {
				// 判断手机平台是 Android 还是 IOS
				if (uni.getSystemInfoSync().platform === 'android') {
					// console.log('运行Android上')
					uni.showModal({
						title:"保存成功，地址为",
						content:"我的手机/Android/data/com.tencent.mm/MicroMsg/wxanewfiles"
					})
				} else {
					// console.log('运行iOS上')
					uni.showToast({
						title:"请预览后点击右上角下载",
						icon:"none",
						duration:2000
					})
				}
              } else {
                uni.showToast({
                  title: "保存失败",
                  icon: "error"
                });
              }
            },
            fail: function(errs) {
              uni.hideLoading();
              uni.showToast({
                title: "下载失败",
                icon: "error"
              });
            }
          });
        },
        fail: function(err) {
          uni.hideLoading();
          uni.showToast({
            title: "下载失败",
            icon: "error"
          });
        },
        complete: function() {
          uni.hideLoading();
        }
      });
    } else {
      uni.hideLoading();
      console.log('不支持的文件类型');
    }
  }
};

export { FileHandler };